﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
{
    class SceneObject : BaseObject
    {
        public bool m_bRested;

        public SceneObject()
        {
            m_ObjectType = OBJ_TYPE.OBJ_SCENEOBJECT;

            m_Position = new Vector2(500, 100);
            m_AnimOrigin = new Vector2(0, 0);
            m_nMaxFrame = 1;
            m_bRested = false;
            m_fDepth = 51;
        }

        override public void Update(float deltaT, KeyboardState LastKeyState)
        {
            m_Position.Y += m_Velocity.Y * deltaT;
            m_Position.X += m_Velocity.X * deltaT;

            if (m_bRested != true)
                    m_Velocity.Y += 9.81f;

            m_bRested = false;

            m_fFrame += deltaT * ((m_Velocity.X > 0.0f) ? m_Velocity.X : -m_Velocity.X) / 25.0f;
            if (m_fFrame >= m_nMaxFrame)
                m_fFrame = 0;
        }

        override public void Render(SpriteBatch spriteBatch, Camera cam)
        {
            spriteBatch.Draw(m_Texture, new Rectangle((int)((m_Position.X - cam.m_Position.X) * cam.m_fZoom), (int)((m_Position.Y - cam.m_Position.Y) * cam.m_fZoom), (int)(128 * cam.m_fZoom), (int)(128 * cam.m_fZoom)), new Rectangle(128 * (int)m_fFrame + (int)m_AnimOrigin.X, (int)m_AnimOrigin.Y, 128, 128), Color.White, 0.0f, new Vector2(64, 128), m_Direction, 0.0f);
        }

        public override void CollisionResponse(BaseObject other)
        {
            if (other.m_ObjectType == OBJ_TYPE.OBJ_SCENEOBJECT)
            {
                Rectangle myRect = new Rectangle((int)m_Position.X - 64, (int)m_Position.Y - 128, 128, 100);
                Rectangle myFeet = new Rectangle((int)m_Position.X - 1, (int)m_Position.Y - 28, 2, 28);
                Rectangle otherRect = new Rectangle((int)other.m_Position.X - 64, (int)other.m_Position.Y - 128, 128, 128);
                if (myFeet.Intersects(otherRect))
                {
                    Rectangle collRect = Rectangle.Intersect(myFeet, otherRect);
                    m_Position.Y += collRect.Height * Math.Sign(m_Position.Y - other.m_Position.Y);
                    m_Velocity.Y = 0.0f;
                    m_bRested = true;
                }
                else if (myRect.Intersects(otherRect))
                {
                    Rectangle collRect = Rectangle.Intersect(myRect, otherRect);

                    //m_Position.Y += (collRect.Height+1) * Math.Sign(m_Position.Y - other.m_Position.Y);
                    m_Position.X += (collRect.Width + 1) * Math.Sign(m_Position.X - other.m_Position.X);
                }
            }
            else if (other.m_ObjectType == OBJ_TYPE.OBJ_TILE)
            {
                if (!LevelManager.Instance.m_TileProperties[((Tile)other).m_nTileID].m_bIsSolid)
                    return;

                Rectangle myFeet = new Rectangle((int)m_Position.X - 16, (int)m_Position.Y - 28, 32, 28);
                Rectangle myRect = new Rectangle((int)m_Position.X - 32, (int)m_Position.Y - 128, 64, 100);
                Rectangle tileRect = new Rectangle((int)other.m_Position.X, (int)other.m_Position.Y, (int)LevelManager.Instance.m_TileSize.X, (int)LevelManager.Instance.m_TileSize.Y);

                if (myFeet.Intersects(tileRect))
                {
                    Rectangle collRect = Rectangle.Intersect(myFeet, tileRect);
                    if (m_Velocity.Y >= 0.0)
                    {
                        m_Position.Y -= (collRect.Height - 1) * Math.Sign(m_Position.Y - other.m_Position.Y);
                        m_bRested = true;
                        m_Velocity.Y = 0.0f;
                    }
                }
                else if (myRect.Intersects(tileRect))
                {
                    Rectangle collRect = Rectangle.Intersect(myRect, tileRect);
                    Vector2 Angle = new Vector2(myRect.Center.Y - tileRect.Center.Y, myRect.X - tileRect.X);
                    if (collRect.Width > collRect.Height)
                    {
                        m_Position.Y += (collRect.Height + 1) * Math.Sign(m_Position.Y - other.m_Position.Y);
                        if (m_Velocity.Y < 0.0f)
                            m_Velocity.Y = 0.0f;
                    }
                    else
                        m_Position.X += (collRect.Width + 1) * Math.Sign(m_Position.X - other.m_Position.X);
                }
            }
        }
    }
}
